<?php

class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{

    protected function _initView()
    {
        // Initialize view


        $this->bootstrap('layout');

        Zend_Controller_Action_HelperBroker::addPrefix('My_Controller_Action_Helper');
        //Zend_Controller_Action_HelperBroker::addPath($path)

        $layout = $this->getResource('layout');

        $view = $layout->getView();


        $view->doctype('XHTML1_TRANSITIONAL');
        $view->headTitle('Ultimate Playlist');
        $view->headTitle()->setSeparator(' :: ');
        $view->setUseStreamWrapper(true);
        $view->headMeta()->appendHttpEquiv('Content-Type', 'text/html; charset=UTF-8');

        // Add it to the ViewRenderer
        $viewRenderer = Zend_Controller_Action_HelperBroker::getStaticHelper(
                        'ViewRenderer'
        );
        $viewRenderer->setView($view);

        $view->addHelperPath(APPLICATION_PATH . '/../library/My/View/Helper', 'My_View_Helper');




        // Return it, so that it can be stored by the bootstrap
        return $view;
    }

    protected function _initRoutes()
    {

        //ROUTING
        $this->bootstrap('frontController');
        $front = Zend_Controller_Front::getInstance();
        $router = $this->frontController->getRouter();

        $router->addConfig(new Zend_Config_Ini(APPLICATION_PATH . '/configs/routes.ini'));
    }

    /**
     * Automaticke nacitani modulu
     *
     * @return Zend_Application_Module_Autoloader
     */
    protected function _initAutoload()
    {
        Zend_Loader::loadClass('Zend_Loader_Autoloader');
        $loader = Zend_Loader_Autoloader::getInstance();
        $loader->registerNamespace('My_');
        $loader->setFallbackAutoloader(true);


        $resourceLoader = new Zend_Loader_Autoloader_Resource(array(
                    'basePath' => APPLICATION_PATH,
                    'namespace' => '',
                    'resourceTypes' => array(
                        'acl' => array(
                            'path' => 'acls/',
                            'namespace' => 'Acl',
                        ),
                        'form' => array(
                            'path' => 'forms/',
                            'namespace' => 'Form',
                        ),
                        'model' => array(
                            'path' => 'models/',
                            'namespace' => 'Model'
                        ),
                    ),
                ));

        $loader->pushAutoloader($resourceLoader);

        return $loader;
    }

    /**
     * Prida do include path adresar s modely
     */
    protected function _initIncludePath()
    {
        $rootDir = dirname(dirname(__FILE__));

        set_include_path(get_include_path()
                . PATH_SEPARATOR . $rootDir . '/library'
                . PATH_SEPARATOR . $rootDir . '/application/models'
        );

        Debug::enable();
    }

    protected function _initTranslate()
    {
        $session = new Zend_Session_Namespace('language');

        $language = $session->language;

        if (empty($language))
        {
            if (User::isActiveUser())
            {
                $user = User::getActiveUser();
                $language = $user->getLocale();
            }
        }

        $allowed = array('cs', 'en');

        if (empty($language) || !in_array($language, $allowed))
        {
            $language = 'en';
        }

        $filename = APPLICATION_PATH . '/translate/' . $language . '.csv';
        $translate = new Zend_Translate('csv', $filename, $language);
        $translate->setLocale($language);

        Zend_Registry::set('Zend_Translate', $translate);
        Zend_Form::setDefaultTranslator($translate);
    }

    protected function _initNavigation()
    {
        $this->bootstrap('layout');

        $is_reg = false;
        $is_log = false;

        if (Zend_Auth::getInstance()->hasIdentity())
        {
            $is_reg = true;
            $is_log = true;
        }
        else
        {
            $session = new Zend_Session_Namespace('fm');
            if (!empty($session->fm))
            {
                $is_log = true;
            }
        }


        $navigation = new Zend_Navigation();


        $page = new Zend_Navigation_Page_Mvc(array(
                    'action' => 'index',
                    'controller' => 'index',
                    'label' => 'Home',
                    'route' => 'default'
                ));

        $navigation->addPage($page);


        if ($is_reg)
        {
            $page = new Zend_Navigation_Page_Mvc(array(
                        'action' => 'my-playlist',
                        'controller' => 'playlist',
                        'label' => 'My Playlists',
                        'route' => 'default'
                    ));

            $navigation->addPage($page);
        }



        if ($is_log)
        {
            $page = new Zend_Navigation_Page_Mvc(array(
                        'action' => 'favourite-playlist',
                        'controller' => 'playlist',
                        'label' => 'Favourite Playlist',
                        'route' => 'default'
                    ));

            $navigation->addPage($page);
        }



        if ($is_reg)
        {
            $page = new Zend_Navigation_Page_Mvc(array(
                        'action' => 'account',
                        'controller' => 'user',
                        'label' => 'My Account',
                        'route' => 'default'
                    ));

            $navigation->addPage($page);
        }

        if (!$is_reg)
        {
            $page = new Zend_Navigation_Page_Mvc(array(
                        'action' => 'register',
                        'controller' => 'user',
                        'label' => 'Register',
                        'route' => 'default'
                    ));

            $navigation->addPage($page);
        }



        $page = new Zend_Navigation_Page_Mvc(array(
                    'route' => 'credits',
                    'action' => 'credits',
                    'controller' => 'index',
                    'label' => 'Credits'
                ));

        $navigation->addPage($page);

        $page = new Zend_Navigation_Page_Mvc(array(
                    'route' => 'default',
                    'action' => 'faq',
                    'controller' => 'index',
                    'label' => 'FAQ'
                ));

        $navigation->addPage($page);



        if ($is_log)
        {
            $page = new Zend_Navigation_Page_Mvc(array(
                        'action' => 'logout',
                        'controller' => 'user',
                        'label' => 'Logout',
                        'route' => 'default'
                    ));

            $navigation->addPage($page);
        }




        $layout = $this->getResource('layout');

        $view = $layout->getView();




        $this->view->navigation($navigation);
        $this->view->nav = $navigation;
        $this->view->is_log = $is_log;
        $this->view->is_reg = $is_reg;
    }

}

